package com.seaboxdata.threadHandler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.RegionEbill;
import com.seaboxdata.kafka.BaseStringKafkaProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author
 * @create 2021-05-25 11:37
 **/
@Component
@Slf4j
public class RegionEbillHandler implements MessageListener {

    @Autowired
    private BaseStringKafkaProducer baseStringKafkaProducer;

    @Value("${jfxt.regionEbill.topic}")
    private String regionEbillTopic;

    @Override
    public void onMessage(Message message) {
        try {
            RegionEbill regionEbill = JSON.parseObject(((TextMessage) message).getText(), RegionEbill.class);
            if(regionEbill != null){
                regionEbill.setCjsj(new Date());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                SimpleDateFormat sdfOld = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                if(!StringUtils.isEmpty(regionEbill.getCreateTime())){
                    regionEbill.setCreateTime(sdf.format(sdfOld.parse(regionEbill.getCreateTime())));
                }

                if(!StringUtils.isEmpty(regionEbill.getFinishTime())){
                    regionEbill.setFinishTime(sdf.format(sdfOld.parse(regionEbill.getFinishTime())));
                }

                if(!StringUtils.isEmpty(regionEbill.getEixtSiteTime())){
                    regionEbill.setEixtSiteTime(sdf.format(sdfOld.parse(regionEbill.getEixtSiteTime())));
                }

                if(!StringUtils.isEmpty(regionEbill.getDisposalSignTime())){
                    regionEbill.setDisposalSignTime(sdf.format(sdfOld.parse(regionEbill.getDisposalSignTime())));
                }

                if(!StringUtils.isEmpty(regionEbill.getSiteSignTime())){
                    regionEbill.setSiteSignTime(sdf.format(sdfOld.parse(regionEbill.getSiteSignTime())));
                }
                baseStringKafkaProducer.sendMessageToKafka(JSONObject.toJSONString(regionEbill), regionEbillTopic);
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("regionEbill get message error");
        }
    }
}
